package org.example.batchInsert;

import java.sql.*;
import java.util.Random;
import java.util.UUID;

public class BatchInsertCustomerData {

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/test5";
        String username = "root";
        String password = "123456";

        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            // 1. 加载驱动并获取数据库连接
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            connection.setAutoCommit(false); // 关闭自动提交

            // 2. 准备SQL语句
            String sql = "INSERT INTO bo_busi_customer (" +
                    "cust_id, cust_type, cust_name, nick_name, cust_email, mobile_phone, " +
                    "root_user_id, id_card_type, id_card_no, origin_channel, sale_channel, " +
                    "is_agent_cust, industry_id, cust_level, consumption_level, cur_manager_id, " +
                    "cur_maintain_id, status_cd, status_date, province_id, city_id, branch_id, " +
                    "create_staff, create_date, update_staff, update_date, remark, department_id, " +
                    "cust_cloud_type, next_follow_date, is_open_sea, is_main_account, " +
                    "register_channel_type, register_channel_name, key_word, latest_clue_time" +
                    ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            preparedStatement = connection.prepareStatement(sql);

            // 3. 随机数生成器
            Random random = new Random();
            long baseTimestamp = System.currentTimeMillis();

            // 4. 批量插入5万条数据
            int batchSize = 1000;
            int totalRecords = 100000;

            for (int i = 1; i <= totalRecords; i++) {
                // 生成UUID作为主键（去掉短横线）
                String custId = UUID.randomUUID().toString().replace("-", "");

                // 设置消费等级（V0-V10随机）
                String consumptionLevel = "V" + random.nextInt(11);

                // 设置参数
                preparedStatement.setString(1, custId);                       // cust_id
                preparedStatement.setString(2, "CUS_" + random.nextInt(100)); // cust_type
                preparedStatement.setString(3, "客户" + i);                    // cust_name
                preparedStatement.setString(4, "昵称" + i);                    // nick_name
                preparedStatement.setString(5, "email" + i + "@example.com"); // cust_email
                preparedStatement.setString(6, "1" + random.nextInt(1000000000)); // mobile_phone
                preparedStatement.setString(7, "root" + random.nextInt(100)); // root_user_id
                preparedStatement.setString(8, "IDC" + random.nextInt(10));   // id_card_type
                preparedStatement.setString(9, "IDNO" + random.nextInt(1000000)); // id_card_no
                preparedStatement.setString(10, "ORG" + random.nextInt(5));   // origin_channel
                preparedStatement.setString(11, "SALE" + random.nextInt(5));  // sale_channel
                preparedStatement.setString(12, random.nextBoolean() ? "Y" : "N"); // is_agent_cust
                preparedStatement.setString(13, "IND" + random.nextInt(20));  // industry_id
                preparedStatement.setString(14, "L" + random.nextInt(5));     // cust_level
                preparedStatement.setString(15, consumptionLevel);            // consumption_level
                preparedStatement.setString(16, "mgr" + random.nextInt(100)); // cur_manager_id
                preparedStatement.setString(17, "mtn" + random.nextInt(100)); // cur_maintain_id
                preparedStatement.setString(18, "STATUS" + random.nextInt(3)); // status_cd
                preparedStatement.setLong(19, baseTimestamp - random.nextInt(10000000)); // status_date
                preparedStatement.setString(20, "PROV" + random.nextInt(10)); // province_id
                preparedStatement.setString(21, "CITY" + random.nextInt(20)); // city_id
                preparedStatement.setString(22, "BRNCH" + random.nextInt(30)); // branch_id
                preparedStatement.setString(23, "creator" + random.nextInt(10)); // create_staff
                preparedStatement.setLong(24, baseTimestamp - random.nextInt(10000000)); // create_date
                preparedStatement.setString(25, "updater" + random.nextInt(10)); // update_staff
                preparedStatement.setLong(26, baseTimestamp - random.nextInt(5000000)); // update_date
                preparedStatement.setString(27, "备注信息" + i);                // remark
                preparedStatement.setString(28, "DEPT" + random.nextInt(10)); // department_id
                preparedStatement.setString(29, "CLOUD" + random.nextInt(4)); // cust_cloud_type
                preparedStatement.setLong(30, baseTimestamp + random.nextInt(100000000)); // next_follow_date
                preparedStatement.setString(31, random.nextBoolean() ? "Y" : "N"); // is_open_sea
                preparedStatement.setString(32, random.nextBoolean() ? "Y" : "N"); // is_main_account
                preparedStatement.setString(33, "REG" + random.nextInt(3));   // register_channel_type
                preparedStatement.setString(34, "渠道" + random.nextInt(5));   // register_channel_name
                preparedStatement.setString(35, "关键词" + random.nextInt(100)); // key_word
                preparedStatement.setLong(36, baseTimestamp - random.nextInt(20000000)); // latest_clue_time

                preparedStatement.addBatch();

                // 批量提交
                if (i % batchSize == 0 || i == totalRecords) {
                    preparedStatement.executeBatch();
                    connection.commit();
                    System.out.println("已插入: " + i + "/" + totalRecords + " 条记录");
                }
            }

            System.out.println("数据插入完成！共插入 " + totalRecords + " 条记录");

        } catch (Exception e) {
            e.printStackTrace();
            try {
                if (connection != null) connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            // 5. 关闭资源
            try {
                if (preparedStatement != null) preparedStatement.close();
                if (connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}